﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.BidSystem.Model;

public partial class Manage_CheckThreePwd : System.Web.UI.Page
{

    string action;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        action = (Request["action"] ?? "bid").ToLower(); // 默认报价开关 dec是解密开关

        Page.Form.DefaultButton = btnSubmit.UniqueID;
        if (!IsPostBack)
        {


            CurrentIndex = 0;
            SetBind();
        }


    }

    private void SetBind()
    {

        IList<ThreePwdConfig> list = BLL_Factory.IBLL_Manage.GetThreePwdConfig(CurrentAccount.ProjectId);

        if (CurrentIndex >= list.Count)
        {
            int i = 0;
            switch (action)
            {
                case "dec":
                    i = BLL_Factory.IBLL_Manage.SetProjectConfig(new ProjectConfig() { ProjectId = CurrentAccount.ProjectId, DecSwitch = true });
                    if (i == 1)
                    {

                        HZ.Common.log4netManager.LogInfo("设置解密开关", string.Format("开启解密开关成功，操作人：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP), null);

                        if (SystemConfig.Instance.EncryptType == 1) // 中心端解密
                        {
                            // 首先 清空解密key
                            


                            int n = BLL_Factory.IBLL_Manage.DecByCenter(CurrentAccount.ProjectId);
                            if (n == 0)
                            {
                                HZ.Web.WebUtil.Alert("中心端解密失败，请关闭解密开关后重新打开解密开关重试。", this);
                                HZ.Web.WebUtil.RedirectByScript("SwitchSetting.aspx");
                            }
                        }
                    }
                    break;
                default:
                    i = BLL_Factory.IBLL_Manage.SetProjectConfig(new ProjectConfig() { ProjectId = CurrentAccount.ProjectId, BidSwitch = true });
                    if (i == 1)
                    {

                        HZ.Common.log4netManager.LogInfo("设置报价开关", string.Format("开启报价开关成功，操作人：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP), null);

                    }
                    break;
            }
            if (i == 1)
            {
                Response.Redirect("SwitchSetting.aspx");
            }
            else
            {
                HZ.Common.log4netManager.LogInfo("设置报价开关失败", string.Format("用户名：{0}，登录IP：{1}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP), null);
            }
        }

        lblUserName.Text = list[CurrentIndex].CompeteDecName;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        IList<ThreePwdConfig> list = BLL_Factory.IBLL_Manage.GetThreePwdConfig(CurrentAccount.ProjectId);

        if (string.Compare(list[CurrentIndex].CompeteDecPassword, HZ.Fun.GetMd5(txtPwd.Text.Trim()), true) == 0)
        {
            CurrentIndex = CurrentIndex + 1;

            SetBind();
        }
        else
        {
            HZ.Web.WebUtil.Alert("密码输入错误", this);
        }
    }

    private int CurrentIndex
    {
        get
        {
            return (int)ViewState["CurrentIndex"];
        }
        set
        {
            ViewState["CurrentIndex"] = value;
        }

    }
}
